#include "rtc_RX8010.h"
#include "board.h"
#include "fsl_debug_console.h"
uint8_t bcd2bin(uint8_t data)
{
	int i=0;
	uint8_t ret_Val = 0;
	uint8_t mask[4] = {0x01,0x02,0x04,0x08};
//////////////LOW
	for(i=0;i<4;i++)
	{
		if(mask[i]&data)
		{
				ret_Val+=mask[i];
		}
	}
	///////////HIGH
		for(i=0;i<4;i++)
	{
		if(mask[i]&(data>>4))
		{
			ret_Val+=(mask[i]*10);
		}
	}

	return ret_Val;
}

uint8_t  bin2bcd(uint8_t dataH,uint8_t dataL)
{
	return (dataH<<4)|dataL;
}
int rx8010_init()
{
	uint8_t flag = 0;
	uint8_t data = 0;
	uint8_t ctrl[2];
	int need_clear = 0, err = 0;

	err = BOARD_RTC_I2C_Receive(RX8010_Address,RX8010_FLAG,0x01,&flag,1);

	flag &= ~(RX8010_FLAG_VLF);

	err = BOARD_RTC_I2C_Send(RX8010_Address,RX8010_FLAG,0x01,&flag,1);

	    /* Initialize reserved registers as specified in datasheet */

   data = 0xD8;
   err = BOARD_RTC_I2C_Send(RX8010_Address,RX8010_RESV17,0x01,&data,1);

   data = 0x00;
   err = BOARD_RTC_I2C_Send(RX8010_Address,RX8010_RESV30,0x01,&data,1);

   data = 0x08;
   err = BOARD_RTC_I2C_Send(RX8010_Address,RX8010_RESV31,0x01,&data,1);

   data = 0x00;
   err = BOARD_RTC_I2C_Send(RX8010_Address,RX8010_IRQ,0x01,&data,1);

   err = BOARD_RTC_I2C_Receive(RX8010_Address,RX8010_FLAG,0x01,ctrl,2);


	if (ctrl[0] & RX8010_FLAG_VLF)
// dev_warn(&client->dev, "Frequency stop was detected\n");
		PRINTF("\r\n Frequency stop was detected\r\n");

    if (ctrl[0] & RX8010_FLAG_AF) {
       // dev_warn(&client->dev, "Alarm was detected\n");
			PRINTF("\r\n Alarm was detected\r\n");
        need_clear = 1;
    }

    if (ctrl[0] & RX8010_FLAG_TF)
        need_clear = 1;

    if (ctrl[0] & RX8010_FLAG_UF)
        need_clear = 1;

    if (need_clear) {
        ctrl[0] &= ~(RX8010_FLAG_AF | RX8010_FLAG_TF | RX8010_FLAG_UF);
        err = BOARD_RTC_I2C_Send(RX8010_Address, RX8010_FLAG,0x01, ctrl,1);
        if (!err)
            return err;
				}
    return err;


}
int get_bcd_date(uint8_t* date,uint8_t* bcd_date)
{
	uint8_t tempBuff[20]= {0};
	uint8_t  temp = 0;
	int j = 0;
  if((date[0]!=0x32)||(date[1]!=0x30)||(date[4]!=0x2D)||(date[7]!=0x2D)||(date[10]!=0x20)||(date[13]!=0x3A)||(date[16]!=0x3A))
	{
	   return -1;
	}
	for(int i=0;i<19;i++)
	{
   if((date[i]>=48)&&(date[i]<=57))
	 {
		 tempBuff[j] = date[i] - 48;
		 j++;
	 }
	}
		if(j!=14)
		{
			return -1;
		}

		bcd_date[5] = bin2bcd(tempBuff[2],tempBuff[3]);

		temp = tempBuff[4]*10 + tempBuff[5];
		if((temp <1)||(temp>12))
		{
			return -1;
		}
		bcd_date[4] = bin2bcd(tempBuff[4],tempBuff[5]);



		temp = tempBuff[6]*10 + tempBuff[7];
		if((temp <1)||(temp>31))
		{
			return -1;
		}
		bcd_date[3] = bin2bcd(tempBuff[6],tempBuff[7]);



		temp = tempBuff[8]*10 + tempBuff[9];
		if((temp <1)||(temp>24))
		{
			return -1;
		}
		bcd_date[2] = bin2bcd(tempBuff[8],tempBuff[9]);



		temp = tempBuff[10]*10 + tempBuff[11];
		if((temp <1)||(temp>59))
		{
			return -1;
		}
		bcd_date[1] = bin2bcd(tempBuff[10],tempBuff[11]);


		temp = tempBuff[12]*10 + tempBuff[13];
		if((temp <1)||(temp>59))
		{
			return -1;
		}
		bcd_date[0] = bin2bcd(tempBuff[12],tempBuff[13]);


	return 0;
}

int rx8010_get_time(uint8_t year_h,uint8_t year_l,uint8_t month_h,
					uint8_t month_l,uint8_t day_h,uint8_t day_l,
					uint8_t hour_h,uint8_t hour_l,uint8_t minutes_h,
					uint8_t minutes_l,uint8_t sec_h,uint8_t sec_l)
{
   uint8_t date[7];
	  uint8_t dateRsul[7];
   uint8_t flagreg;
   int err;

   err = BOARD_RTC_I2C_Receive(RX8010_Address, RX8010_FLAG,0x01,&flagreg,1);

   if (flagreg & RX8010_FLAG_VLF)
			{
			  PRINTF("\r\n Frequency stop was detected\r\n");
       return 1;
     }

   err = BOARD_RTC_I2C_Receive(RX8010_Address, RX8010_SEC,0x01,date,7);

   dateRsul[0] = bcd2bin(date[RX8010_SEC - RX8010_SEC] & 0x7f);
   dateRsul[1] = bcd2bin(date[RX8010_MIN - RX8010_SEC] & 0x7f);
   dateRsul[2] = bcd2bin(date[RX8010_HOUR - RX8010_SEC] & 0x3f);
   dateRsul[4] = bcd2bin(date[RX8010_MDAY - RX8010_SEC] & 0x3f);
   dateRsul[5] = bcd2bin(date[RX8010_MONTH - RX8010_SEC] & 0x1f);
   dateRsul[6] = bcd2bin(date[RX8010_YEAR - RX8010_SEC]);
   dateRsul[3] = date[RX8010_WDAY - RX8010_SEC] & 0x7f;

//   PRINTF("\r\n Read datetime from RX8010:    20%d%d-%d%d-%d%d  %d%d:%d%d:%d%d   \r\n",
//       dateRsul[6]/10,dateRsul[6]%10,dateRsul[5]/10,dateRsul[5]%10,dateRsul[4]/10,dateRsul[4]%10,
//				dateRsul[2]/10,dateRsul[2]%10,dateRsul[1]/10,dateRsul[1]%10,dateRsul[0]/10,dateRsul[0]%10);
   year_h = dateRsul[6]/10;
   year_l = dateRsul[6]%10;
   month_h = dateRsul[5]/10;
   month_l = dateRsul[5]%10;
   day_h = dateRsul[4]/10;
   day_l = dateRsul[4]%10;
   hour_h = dateRsul[2]/10;
   hour_l = dateRsul[2]%10;
   minutes_h = dateRsul[1]/10;
   minutes_l = dateRsul[1]%10;
   sec_h = dateRsul[0]/10;
   sec_l = dateRsul[0]%10;

   return 0;
}
int rx8010_set_time(uint8_t *asc_date)
{
   uint8_t bcd_date[6];
   int err;

   if(get_bcd_date(asc_date,bcd_date))
   {
	   	   PRINTF("\r\n Date format error!!!!!!!! \r\n");
	   	   return -1;
   }

	 //	2018-06-21 16:29:30

   err = BOARD_RTC_I2C_Send(RX8010_Address,RX8010_SEC,0x01,bcd_date,3);

   err = BOARD_RTC_I2C_Send(RX8010_Address,RX8010_MDAY,0x01,&bcd_date[3],3);

}
